package com.apestech.framework.dictionary;

import com.apestech.framework.db.SqlSession;
import com.apestech.framework.dictionary.modul.Table;
import com.apestech.framework.json.JSONObject;
import com.apestech.framework.util.FileUtil;
import com.apestech.framework.util.MathUtil;
import com.apestech.framework.util.StringUtil;

/**
 * @author hup
 * @create 2018-08-31-9:51
 */
public class DictUtil extends StringUtil{

    private static String DICT_DEFAULT_FILE="D:\\dict\\";

    public static void setDefaultDir(String dir) throws Exception {
        FileUtil.deleteDir(dir);
        DICT_DEFAULT_FILE = dir;
    }

    public static String getDefaultDir(){
        return DICT_DEFAULT_FILE;
    }

    public static String getDictDir(SqlSession session){
        return getDefaultDir()+session.getDbName();
    }

    public static JSONObject getReslutData(String COLUMN_NAME, Object REMARKS, String TYPE_NAME, String COLUMN_DEF, String NULLABLE,
                                    Object CHAR_OCTET_LENGTH, Object COLUMN_SIZE, Object DECIMAL_DIGITS) throws Exception {  //CHAR_OCTET_LENGTH 128   COLUMN_SIZE 32
        JSONObject map=new JSONObject();
        map.put("COLUMN_NAME", COLUMN_NAME);
        map.put("REMARKS", REMARKS);
        String DEF=COLUMN_DEF;
        if(DEF!=null){
            DEF=DEF.trim();
            map.put("COLUMN_DEF", DEF);
        }
        String NULL=NULLABLE;
        if("0".equals(NULL)){
            map.put("NOTNULL", "1");
        }
        map.put("TYPE_NAME", TYPE_NAME);
        switch (TYPE_NAME){
            case "VARCHAR2":{
                map.put("LENGTH", COLUMN_SIZE);
                map.put("CLASSNAME", "java.lang.String");
                if(!CHAR_OCTET_LENGTH.equals(COLUMN_SIZE)){
                    if(MathUtil.div(castToDouble(CHAR_OCTET_LENGTH), castToDouble(COLUMN_SIZE))==4) map.put("DB_TYPE", "VARCHAR2("+COLUMN_SIZE+" CHAR)");
                } else map.put("DB_TYPE", "VARCHAR2("+COLUMN_SIZE+")");
                break;
            }
            case "NVARCHAR2":{
                map.put("LENGTH", COLUMN_SIZE);
                map.put("CLASSNAME", "java.lang.String");
                map.put("DB_TYPE", "NVARCHAR2("+COLUMN_SIZE+")");
                break;
            }
            case "CHAR":{
                map.put("LENGTH", COLUMN_SIZE);
                map.put("CLASSNAME", "java.lang.String");
                map.put("DB_TYPE", "CHAR("+COLUMN_SIZE+")");
                break;
            }
            case "NUMBER":{
                map.put("LENGTH", COLUMN_SIZE);
                map.put("SIZE", DECIMAL_DIGITS);
                map.put("CLASSNAME", "java.math.BigDecimal");
                if(DECIMAL_DIGITS==null || castToInt(DECIMAL_DIGITS)==0) map.put("DB_TYPE", "NUMBER("+COLUMN_SIZE+")");
                else map.put("DB_TYPE", "NUMBER("+COLUMN_SIZE+","+DECIMAL_DIGITS+")");
                break;
            }
            case "TIMESTAMP(6)":{
                map.put("CLASSNAME", "oracle.sql.TIMESTAMP");
                map.put("DB_TYPE", "TIMESTAMP(6)");
                break;
            }
            case "DATE":{
                map.put("CLASSNAME", "java.sql.Timestamp");
                map.put("DB_TYPE", "DATE");
                break;
            }
        }
        return map;
    }


}
